package XynLeen.GraduationProject.Activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonAdapter;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.List;

import XynLeen.GraduationProject.Bean.ArrangementBean;
import XynLeen.GraduationProject.R;
import XynLeen.GraduationProject.view.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_Arrangement extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    @Bind(R.id.list_view)
    ListView mListView;
    @Bind(R.id.ib_navigation_back)
    ImageButton mIbNavigation_back;
    @Bind(R.id.ib_navigation_add)
    ImageButton mIbNavigation_add;
    @Bind(R.id.tv_navigation_label)
    TextView mTvNavigation;

    public CommonAdapter<ArrangementBean> quickAdapter;

    public SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        initQuickAdapter();
        initData();
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);

        mTvNavigation.setText("流程安排");

    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    public void initQuickAdapter() {

        quickAdapter = new CommonAdapter<ArrangementBean>(this, R.layout.adapter_listview_arrangements, null) {

            @Override
            public void onUpdate(BaseAdapterHelper helper, ArrangementBean bean, int position) {

                TextView mDate = helper.getView(R.id.fieldname_date);
                mDate.setText(bean.getTime());
                TextView mTitle = helper.getView(R.id.fieldname_title);
                mTitle.setText(bean.getTitle());
                TextView mContent=helper.getView(R.id.fieldname_content);
                mContent.setText(bean.getContent());
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            }
        };
    }

    public void initData() {

        List<ArrangementBean> list = DataSupport.order("time").find(ArrangementBean.class);
        quickAdapter.clear();
        mListView.setAdapter(quickAdapter);
        quickAdapter.addAll(list);
        quickAdapter.notifyDataSetChanged();

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final ArrangementBean bean = quickAdapter.getItem(position);
        Intent intent = new Intent(this, Activity_Arrangement_Add.class);
        intent.putExtra("bean", bean);
        intent.putExtra("title", "修改");
        startActivity(intent);
    }

    @OnClick({R.id.ib_navigation_back,R.id.ib_navigation_add})
    public void OnClick(View v){
        switch (v.getId()){
            case R.id.ib_navigation_back:
                finish();
                break;
            case R.id.ib_navigation_add:
                Intent intent = new Intent(this, Activity_Arrangement_Add.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_arrangement;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initView() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
